package com.kotlin.zds.fragment

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.kotlin.zds.R
import com.kotlin.zds.arouter.Routes
import com.kotlin.zds.databinding.FragmentMainBinding
import com.kotlin.zds.databinding.ItemMainListBinding
import com.kotlin.zds.qrcode.CustomCaptureActivity
import com.kotlin.zds.utils.XToastUtils
import com.xuexiang.xqrcode.XQRCode
import com.zds.support.adapter.CommonRecyclerAdapter
import com.zds.support.api.resp.RecordMsg
import com.zds.support.base.SupportBaseFragment

/**
 * Author: zds
 * Date: 2022/6/11 13:09
 * Description: fragment实现列表
 * Version: v1.0
 */
class MainFrament : SupportBaseFragment() {
    val REQUEST_CODE = 111
    private var _viewBinding : FragmentMainBinding?=null
    private val viewBinding get() = _viewBinding!!
    override fun getContentView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = FragmentMainBinding.inflate(inflater,container,false)
        _viewBinding = binding
        return binding.root
    }

    var currentPage = 1
    var data = ArrayList<RecordMsg>()
    val adapter by lazy {
        object : CommonRecyclerAdapter<ItemMainListBinding, RecordMsg>(requireActivity(), data) {
            override fun getViewBinding(parent: ViewGroup?):  ItemMainListBinding{
                return ItemMainListBinding.inflate(mLayoutInflater, parent, false)
            }
            override fun convert(viewBinding: ItemMainListBinding, data: RecordMsg) {
                with(viewBinding){
                    tvTitle.text = data.title
                    ly.setOnClickListener{
                        if (data.url==Routes.PATH_WEB){
                            Routes.navicat(data.url,"https://www.baidu.com/")
                        }else if (data.url==Routes.PATH_SCAN){
                            CustomCaptureActivity.start(
                                this@MainFrament,
                                REQUEST_CODE,
                                R.style.XQRCodeTheme_Custom
                            )
                        }else {
                            Routes.navicat(data.url)
                        }
                    }
                }
            }
        }
    }
    override fun initView() {
        viewBinding.run {
            rvMsg.adapter = adapter
            smartRefreshLayout.apply {
                setOnRefreshListener {
                    data.clear()
                    currentPage = 1
                    getList()
                }
                setOnLoadMoreListener {
                    currentPage++
                    data.clear()
                    getList()
                }
                autoRefresh()
            }
        }
    }

    override fun initDta() {

    }

    override fun lazyLoadData() {

    }

    fun getList(){
        data.add(RecordMsg("高德地图集成",Routes.PATH_GAODE))
        data.add(RecordMsg("webview使用",Routes.PATH_WEB))
        data.add(RecordMsg("百度语音集成",Routes.PATH_BAIDU))
        data.add(RecordMsg("recycleview网格布局",Routes.PATH_RVGV))
        data.add(RecordMsg("获取定位",Routes.PATH_GAODE2))
        data.add(RecordMsg("RFID蓝牙连接和数据读取",Routes.PATH_GAODE2))
        data.add(RecordMsg("自定义扫码识别",Routes.PATH_SCAN))
        data.add(RecordMsg("拍照和读取照片",Routes.PATH_PHOTO))

        viewBinding.smartRefreshLayout.finishRefresh()
        viewBinding.smartRefreshLayout.finishLoadMore()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        //处理二维码扫描结果
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            handleScanResult(data)
        }
    }

    /**
     * 处理二维码扫描结果
     *
     * @param data
     */
    private fun handleScanResult(data: Intent?) {
        if (data != null) {
            val bundle = data.extras
            if (bundle != null) {
                if (bundle.getInt(XQRCode.RESULT_TYPE) == XQRCode.RESULT_SUCCESS) {
                    val result = bundle.getString(XQRCode.RESULT_DATA)
                    XToastUtils.toast("解析结果:$result", Toast.LENGTH_LONG)
                } else if (bundle.getInt(XQRCode.RESULT_TYPE) == XQRCode.RESULT_FAILED) {
                    XToastUtils.toast("解析二维码失败", Toast.LENGTH_LONG)
                }
            }
        }
    }
}